package icu.qx.pbs.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import icu.qx.common.constant.TableNameEnum;
import icu.qx.common.utils.CodeUtils;
import icu.qx.pbs.domain.PbsWorkGroupPeo;
import icu.qx.pbs.mapper.PbsWorkGroupPeoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import icu.qx.pbs.mapper.PbsWorkGroupMapper;
import icu.qx.pbs.domain.PbsWorkGroup;
import icu.qx.pbs.service.IPbsWorkGroupService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生产班组Service业务层处理
 *
 * @author yong
 * @date 2024-06-19
 */
@Service
public class PbsWorkGroupServiceImpl implements IPbsWorkGroupService {
    @Autowired
    private PbsWorkGroupMapper pbsWorkGroupMapper;

    @Autowired
    private PbsWorkGroupPeoMapper pbsWorkGroupPeoMapper;

    @Autowired
    private CodeUtils codeUtils;

    /**
     * 查询生产班组
     *
     * @param wgCode 生产班组主键
     * @return 生产班组
     */
    @Override
    public PbsWorkGroup selectPbsWorkGroupByWgCode(String wgCode) {
        List<PbsWorkGroupPeo> pbsWorkGroupPeos = pbsWorkGroupPeoMapper.selectPbsWorkGroupPeoByWgroupWgCode(wgCode);
        PbsWorkGroup pbsWorkGroup = pbsWorkGroupMapper.selectPbsWorkGroupByWgCode(wgCode);
        pbsWorkGroup.setPbsWorkGroupPeos(pbsWorkGroupPeos);
        return pbsWorkGroup;
    }

    /**
     * 查询生产班组列表
     *
     * @param pbsWorkGroup 生产班组
     * @return 生产班组
     */
    @Override
    public List<PbsWorkGroup> selectPbsWorkGroupList(PbsWorkGroup pbsWorkGroup) {
        return pbsWorkGroupMapper.selectPbsWorkGroupList(pbsWorkGroup);
    }

    /**
     * 新增生产班组
     *
     * @param pbsWorkGroup 生产班组
     * @return 结果
     */
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public int insertPbsWorkGroup(PbsWorkGroup pbsWorkGroup) {
        String code = codeUtils.generateCode(TableNameEnum.PBS_WORK_GROUP.getTableName());
        pbsWorkGroup.setWgCode(code);
        pbsWorkGroup.setCreateBy(SecurityUtils.getUsername());
        pbsWorkGroup.setCreateTime(DateUtils.getNowDate());
        // 添加人员
        if (pbsWorkGroup.getPbsWorkGroupPeos() != null) {
            List<PbsWorkGroupPeo> pbsWorkGroupPeos = pbsWorkGroup.getPbsWorkGroupPeos();
            pbsWorkGroupPeos.forEach(e -> {
                e.setWgroupWgCode(code);
                pbsWorkGroupPeoMapper.insertPbsWorkGroupPeo(e);
            });
        }
        return pbsWorkGroupMapper.insertPbsWorkGroup(pbsWorkGroup);
    }

    /**
     * 修改生产班组
     *
     * @param pbsWorkGroup 生产班组
     * @return 结果
     */
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class})
    @Override
    public int updatePbsWorkGroup(PbsWorkGroup pbsWorkGroup) {
        String code = pbsWorkGroup.getWgCode();
        pbsWorkGroup.setUpdateBy(SecurityUtils.getUsername());
        pbsWorkGroup.setUpdateTime(DateUtils.getNowDate());
        // 删除人员
        pbsWorkGroupPeoMapper.deletePbsWorkGroupPeoByWgroupWgCode(code);
        // 添加人员
        if (pbsWorkGroup.getPbsWorkGroupPeos() != null) {
            List<PbsWorkGroupPeo> pbsWorkGroupPeos = pbsWorkGroup.getPbsWorkGroupPeos();
            pbsWorkGroupPeos.forEach(e -> {
                e.setWgroupWgCode(code);
                pbsWorkGroupPeoMapper.insertPbsWorkGroupPeo(e);
            });
        }
        return pbsWorkGroupMapper.updatePbsWorkGroup(pbsWorkGroup);
    }

    /**
     * 批量删除生产班组
     *
     * @param wgCodes 需要删除的生产班组主键
     * @return 结果
     */
    @Override
    public int deletePbsWorkGroupByWgCodes(String[] wgCodes) {
        return pbsWorkGroupMapper.deletePbsWorkGroupByWgCodes(wgCodes);
    }

    /**
     * 删除生产班组信息
     *
     * @param wgCode 生产班组主键
     * @return 结果
     */
    @Override
    public int deletePbsWorkGroupByWgCode(String wgCode) {
        return pbsWorkGroupMapper.deletePbsWorkGroupByWgCode(wgCode);
    }
}
